﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using TinyLib.Input;
using TinyLib.Sprite;
using TinyLib.Entity;

namespace GodBuilderIdea
{
    public class Goal : Entity
    {
        public enum GoalType
        {
            LONE_SURVIVOR,
            OTHERS_SURVIVE,
            STRONGEST_TRIBE,
            OTHER_PL_SURVIVE,
            OTHER_PL_STRONGEST,
            SIZE //this must always be furthest down
        }

        public GoalType m_goal_type;
        public String m_goal_text = "";
        public string m_player_name = "";

        public bool show_obscured = false;
        public bool show_initially = false;
        public bool show_end = false;

        public bool m_success = false;
        public bool m_cheat_power = false;

        SpriteFont m_font;
        Sprite m_background, m_obscured;

        public Goal(Game game, SpriteBatch sprite_batch)
            : base(game, sprite_batch)
        {
        }

        public void Initialize(GoalType goal_type)
        {
            m_goal_type = goal_type;
            switch (m_goal_type)
            {
                case GoalType.LONE_SURVIVOR:
                    m_goal_text = "You win if your tribe type is the only one to survive.";
                    break;
                case GoalType.OTHERS_SURVIVE:
                    m_goal_text = "You win if both the other players tribes survive. \nYour tribe does not have to survive.";
                    break;
                case GoalType.STRONGEST_TRIBE:
                    m_goal_text = "You win if you have the strongest tribe \n(largest in numbers) at the end of the game.";
                    break;
                case GoalType.OTHER_PL_SURVIVE:
                    m_goal_text = "You win if one of the other players survive.";
                    break;
                case GoalType.OTHER_PL_STRONGEST:
                    m_goal_text = "You win if one of the other players are the strongest.";
                    break;
            }


            m_font = Game.Content.Load<SpriteFont>("Font");
            Texture2D tex = Game.Content.Load<Texture2D>("images/quest_bg");
            m_background = new Sprite(Spritebatch, tex, Game);
            m_background.position = new Vector2(400,200);
            tex = Game.Content.Load<Texture2D>("images/quest_obscured");
            m_obscured = new Sprite(Spritebatch, tex, Game);
            m_obscured.position = new Vector2(400,200);

            base.Initialize();
        }

        public void CalcVictoryCondition(List<Player> player_list, List<EnvTile> tile_list)
        {
            int win = 0;
            switch (m_goal_type)
            {
                case GoalType.LONE_SURVIVOR:
                    win = 0;
                    int my_total_tribes = 0;
                    foreach (Player p in player_list){
                        if (p.m_goal.m_player_name == m_player_name)
                        { my_total_tribes = p.m_total_tribes; }
                    }
                    foreach (Player p in player_list)
                    {
                        if (p.m_goal.m_player_name != m_player_name)
                        {
                            if (p.m_tribes.Count > 0)
                            { win = 1;  }
                            else { }
                        }
                    }
                    if (win == 0 && my_total_tribes > 0) { m_success = true; }
                    break;
                case GoalType.OTHERS_SURVIVE:
                    win = 0;
                    foreach (Player p in player_list)
                    {
                        if (p.m_goal.m_player_name != m_player_name)
                        {
                            if (p.m_tribes.Count <= 0) { win = 1; }
                        }
                    }
                    if (win == 0) { m_success = true; }
                    break;
                case GoalType.STRONGEST_TRIBE:
                    win = 0;
                    my_total_tribes = 0;
                    foreach (Player p in player_list){
                        if (p.m_goal.m_player_name == m_player_name)
                        { my_total_tribes = p.m_total_tribes; }
                    }
                    foreach (Player p in player_list) {
                        if (p.m_goal.m_player_name != m_player_name)
                        { if (p.m_total_tribes > my_total_tribes) { win = 1; } }
                    }
                    if (win == 0) { m_success = true; }
                    break;
                case GoalType.OTHER_PL_SURVIVE:
                    win = 0;
                    foreach (Player p in player_list)
                    {
                        if (p.m_goal.m_player_name != m_player_name)
                        { if (p.m_tribes.Count <= 0) { win += 1; } }
                    }
                    if (win < 2) { m_success = true; }
                    break;
                case GoalType.OTHER_PL_STRONGEST:
                    win = 0;
                    my_total_tribes = 0;
                    foreach (Player p in player_list){
                        if (p.m_goal.m_player_name == m_player_name)
                        { my_total_tribes = p.m_total_tribes; }
                    }
                    foreach (Player p in player_list)
                    {
                        if (p.m_goal.m_player_name != m_player_name)
                        { if (p.m_total_tribes < my_total_tribes) { win = 1; } }
                    }
                    if (win == 0) { m_success = true; }
                    break;
            }
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {

            if (show_obscured)
            {
                m_obscured.Draw(gameTime);
                Spritebatch.DrawString(m_font, m_player_name + " can now press [Enter] to see their secret goal", new Vector2(m_obscured.position.X+40,m_obscured.position.Y + 320), Color.White);
            }
            if (show_initially)
            {
                m_background.Draw(gameTime);
                Spritebatch.DrawString(m_font, m_goal_text, new Vector2(m_background.position.X + 50, m_background.position.Y + 50), Color.White);
                if (m_cheat_power)
                {
                    Spritebatch.DrawString(m_font, "You have a secret ability.\nIf you press 'C' at any time in your turn,\na new tribe of your type will spawn a bit later.\nDon't tell your opponent!", new Vector2(m_background.position.X + 50, m_background.position.Y + 125), Color.White);
                }
                Spritebatch.DrawString(m_font, "Press [Enter] to continue", new Vector2(m_background.position.X + 50, m_background.position.Y + 250), Color.White);
            }
            if (show_end)
            {
                //statistics n stuff
            }
            base.Draw(gameTime);
        }
    }
}
